package com.eschool.beans;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import com.eschool.beans.datamodels.RoomDataModel;
import com.eschool.dao.RoomDao;
import com.eschool.model.Room;
import com.eschool.service.SchoolManagementService;
import com.eschool.service.impl.SchoolManagementServiceImpl;
import com.eschool.util.ConstraintsUtil;

@ManagedBean(name="roomStatusUpdateBean")
@ViewScoped
public class RoomStatusUpdateBean {

	private Room[] selectedRoomsList;
    private RoomDataModel roomsModel;
	SchoolManagementService service;	
	private static List<Room> rooms;	
	private boolean newStatus;
	
	public RoomStatusUpdateBean() {
		rooms = new ArrayList<Room>();
		service = new SchoolManagementServiceImpl();		
		//Map  conditions = ConstraintsUtil.getConstraintsMap();
		
		//System.out.println(UserUtil.currentUser().getName() + "   "  + UserUtil.currentUser().getBranch());
		
		//students = StudentDao.getStudentsByBranch(UserUtil.currentUser().getBranch());

		//students = StudentDao.getStudents(null);
		
		rooms = RoomDao.getRooms(ConstraintsUtil.getConstraintsMap());
		
		roomsModel = new RoomDataModel(rooms);
	}

	public boolean isNewStatus() {
		return newStatus;
	}

	public void setNewStatus(boolean newStatus) {
		this.newStatus = newStatus;
	}

	public Room[] getSelectedRoomsList() {
		return selectedRoomsList;
	}

	public void setSelectedRoomsList(Room[] selectedRoomsList) {
		this.selectedRoomsList = selectedRoomsList;
	}

	public RoomDataModel getRoomsModel() {
		return roomsModel;
	}

	public void setRoomsModel(RoomDataModel roomsModel) {
		this.roomsModel = roomsModel;
	}

	public static List<Room> getRooms() {
		return rooms;
	}

	public static void setRooms(List<Room> rooms) {
		RoomStatusUpdateBean.rooms = rooms;
	}

	public String perform(){
    	
    	List selectedRooms = Arrays.asList(selectedRoomsList);

    	service.updateRoomsStatus(selectedRooms, newStatus);
    	
    	FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(
    					FacesMessage.SEVERITY_INFO,"Info message", "Rooms' Availability Updated"));
    	
    	return "roomStatusUpdate";
    }
}
